package org.carabiner.examples;

import javax.swing.DefaultListModel;

import org.carabiner.Carabiner;
import org.jmock.MockObjectTestCase;

/**
 * This is a down-to-the-metal example of how to use the Carabiner framework.
 * Everything in this test is done manually in order to demonstrate how
 * Carabiner works. It is usually not necessary to add all of this code to a
 * normal test.<p/>
 * 
 * What you've got here is two different kinds of testing...unit testing and
 * usability testing...both contained in the same JUnit test. This allows reuse
 * of mock/stub generation code, as well as providing a single, cohesive
 * point-of-entry for the testing of this GUI control. 
 * 
 * @author Ben Rady (benrady@gmail.com)
 */
public class TestMyGuiControl extends MockObjectTestCase
{
  private MyGuiControl control;

  private StubDataModel model;

  protected void setUp() throws Exception
  {
    super.setUp();
    model = new StubDataModel();
    control = new MyGuiControl(model);
  }

  protected void tearDown() throws Exception
  {
    super.tearDown();
  }

  /**
   * This test has nothing to do with Carabiner...it is simply testing the state
   * of the control. These kinds of tests can cover logic in the control that
   * can't be extracted and tested seperately.
   */
  public void testSize()
  {
    control.doLayout();

    // This control must always be less than 500x500.
    assertTrue(control.getSize().getWidth() < 500);
    assertTrue(control.getSize().getHeight() < 500);
  }

  private class StubDataModel extends DefaultListModel implements MyDataModel
  {
    public StubDataModel()
    {
      addElement("Spam");
      addElement("Sausage");
      addElement("Spam");
      addElement("Spam");
      addElement("Bacon");
    }

    public String getStringData()
    {
      StringBuffer buffer = new StringBuffer();
      for (int i = 0; i < size(); i++)
      {
        buffer.append(get(i));
        if (i != size() - 1)
        {
          buffer.append(", ");
        }
      }
      return buffer.toString();
    }
  }

  /**
   * One simple way to run the test harness is to create a main function like
   * this.
   * 
   * @param args
   *          String[]
   * @throws Exception
   * @see {@link ../AbstractCarabinerTestCase.html#showWhenCursed(Class) showWhenCursed}
   */
  public static void main(String[] args) throws Exception
  {
    TestMyGuiControl test = new TestMyGuiControl();
    test.setUp();
    Carabiner.showHarness(test.control);
  }
}
